package java44th.group.controller.admin.coupon;

import java44th.group.bean.admin.bo.coupon.CouponCreateBo;
import java44th.group.bean.admin.bo.coupon.CouponListBo;
import java44th.group.bean.admin.bo.coupon.CouponUserListBo;
import java44th.group.bean.po.MarketCoupon;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.admin.vo.coupon.CouponListVo;
import java44th.group.bean.admin.vo.coupon.CouponUserListVo;
import java44th.group.service.admin.coupon.CouponService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Aurhor: 帅的一批
 * @CreateAt: 2022/10/12 15:22
 * @Description:
 * @PACKAGE_NAME： java44th.group.controller.admin.coupon
 */

@RequestMapping("admin/coupon")
@RestController
public class CouponController {

    @Autowired
    CouponService couponService;

    @RequiresPermissions(value={"admin:coupon:list","*"},logical= Logical.OR)
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public BaseRespVo getCouponList(CouponListBo couponListBo) {
        CouponListVo couponListVo;
        try {
            couponListVo = couponService.getCouponList(couponListBo);
        } catch (Exception e) {
            return  BaseRespVo.invalidParameter("什么玩意?我能报错?");
        }

        return BaseRespVo.ok(couponListVo);
    }
    @RequiresPermissions(value={"admin:coupon:create","*"},logical= Logical.OR)
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public BaseRespVo createCoupon(@RequestBody CouponCreateBo couponCreateBo) {
        MarketCoupon marketCoupon = couponService.createCoupon(couponCreateBo);
        if (marketCoupon == null) {
            return BaseRespVo.invalidParameter("参数有误,插入错误");
        }
        return BaseRespVo.ok(marketCoupon);

    }
    @RequiresPermissions(value={"admin:coupon:update","*"},logical= Logical.OR)
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseRespVo updateCoupon(@RequestBody MarketCoupon marketCoupon) {
        if (!marketCoupon.checkData()) {
            return BaseRespVo.invalidParameter("参数非法");
        }
        marketCoupon = couponService.updateCoupon(marketCoupon);
        return BaseRespVo.ok(marketCoupon);
    }
    @RequiresPermissions(value={"admin:coupon:read","*"},logical= Logical.OR)
    @RequestMapping(value = "read", method = RequestMethod.GET)
    public BaseRespVo getCouponById(Integer id) {
        MarketCoupon marketCoupon = null;
        try {
            marketCoupon = couponService.getCouponById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return  BaseRespVo.invalidParameter("什么玩意?我能报错?");
        }
        return BaseRespVo.ok(marketCoupon);
    }
    @RequiresPermissions(value={"admin:coupon:listuser","*"},logical= Logical.OR)
    @RequestMapping(value = "listuser", method = RequestMethod.GET)
    public BaseRespVo getCouponUserList(CouponUserListBo couponUserListBo) {
        CouponUserListVo couponUserListVo = null;
        try {
            couponUserListVo = couponService.getCouponUserList(couponUserListBo);
        } catch (Exception e) {
            e.printStackTrace();
            return  BaseRespVo.invalidParameter("什么玩意?我能报错?");
        }
        return BaseRespVo.ok(couponUserListVo);
    }

    @PostMapping("delete")
    public BaseRespVo deleteCoupon(@RequestBody Map<String,Object> map){
        couponService.deleteCouponById((int) map.get("id"));
        return BaseRespVo.ok();
    }
}
